﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MDF
{
    /// <summary>
    /// 属性字典
    /// </summary>
    public partial class SXPPZD : CCSkinMain
    {

        private DictionaryInfo m_dicInfo;
        /// <summary>
        /// 属性字典窗体
        /// </summary>
        public SXPPZD()
        {
            InitializeComponent();
            
            //实例化字典信息对象
            m_dicInfo = new DictionaryInfo();
            //填充树视图
            FillSemanticTree();
        }
        /// <summary>
        /// 填充信息
        /// </summary>
        private void FillSemanticTree()
        {
            try
            {
                //隐藏根节点复选框
                TreeNodeCheckBoxs();
                //获取树节点并填充
                this.SemanticTree_TreeView.Nodes.AddRange(m_dicInfo.GetTreeNodesInfo());
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 选择树节点时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SemanticTree_TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                //填充控件文本
                this.NodeName_TextBox.Text = e.Node.Text;
                //查询同义词集
                this.Synset_TextBox.Text = m_dicInfo.GetSynSetInfo(this.SemanticTree_TreeView.SelectedNode.Text, this.SemanticTree_TreeView.SelectedNode.Checked.ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 判断是否修改 勾选框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SemanticTree_TreeView_BeforeCheck(object sender, TreeViewCancelEventArgs e)
        {
            try
            {
                if (this.Query_Button.Enabled)
                {
                    //查询状态，不可修改
                    e.Cancel = true;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 限制输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox m_textBox = (TextBox)sender;
                Regex reg = new Regex("^\\w$");
                if (m_textBox.Text == "" && e.KeyChar >= '0' && e.KeyChar <= '9')
                {
                    e.Handled = true;
                }
                else if (!reg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Query_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //判断查询框的信息是否被修改
                if(this.SemanticTree_TreeView.SelectedNode.Text == this.NodeName_TextBox.Text)
                {
                    //没有修改，则以树节点为准，获取信息
                    this.Synset_TextBox.Text = m_dicInfo.GetSynSetInfo(this.SemanticTree_TreeView.SelectedNode.Text, this.SemanticTree_TreeView.SelectedNode.Checked.ToString());
                }
                else
                {
                    //有修改则以文本框内容，获取信息，默认true
                    this.Synset_TextBox.Text = m_dicInfo.GetSynSetInfo(this.NodeName_TextBox.Text, "True");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Revise_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //获取当前点击的控件
                Button m_button = (Button)sender;
                if(m_button.Text == "修改")
                {
                    //修改状态，则变换控件状态，激活修改命令
                    this.NodeName_TextBox.Text = this.SemanticTree_TreeView.SelectedNode.Text;
                    this.NodeName_TextBox.ReadOnly = true;
                    this.Revise_TextBox.ReadOnly = false;
                    this.Synset_TextBox.ReadOnly = false;
                    this.Query_Button.Enabled = false;
                    this.Create_Button.Enabled = false;
                    m_button.Text = "保存";
                }
                else
                {
                    //保存状态，则变换控件状态，执行保存命令
                    this.NodeName_TextBox.Text = this.SemanticTree_TreeView.SelectedNode.Text;
                    this.NodeName_TextBox.ReadOnly = false;
                    this.Revise_TextBox.ReadOnly = true;
                    this.Synset_TextBox.ReadOnly = true;
                    this.Query_Button.Enabled = true;
                    this.Create_Button.Enabled = true;
                    m_button.Text = "修改";
                    //判断是否为根节点
                    if (this.SemanticTree_TreeView.SelectedNode.Level != 0)
                    {
                        //旧节点名字
                        string oldName = this.NodeName_TextBox.Text;
                        if (this.Revise_TextBox.Text !="")
                        {
                            //新的节点名字
                            string newName = this.Revise_TextBox.Text;
                            //保存节点信息
                            bool result = m_dicInfo.ReviseDicNodeInfo(oldName, newName, this.SemanticTree_TreeView.SelectedNode.Checked.ToString());
                            if (result)
                            {
                                //更新oldname，更新树节点
                                oldName = this.Revise_TextBox.Text;
                                this.SemanticTree_TreeView.SelectedNode.Text = oldName;
                                this.Revise_TextBox.Text = "";
                            }
                        }
                        if (this.Synset_TextBox.Text != "无同义词信息" && this.Synset_TextBox.Text != "")
                        {
                            //保存同义词内容
                            m_dicInfo.ReviseDicSynInfo(oldName, this.Synset_TextBox.Text);
                        }
                        MessageBox.Show("保存完成！", "提示：");
                    }
                    else
                    {
                        MessageBox.Show("不能修改根节点", "警告：");
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 退出字典窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Exit_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //检查当前窗体状态
                if (this.Query_Button.Enabled)
                {
                    //查询状态，正常退出
                    this.Close();
                }
                else
                {
                    //修改状态，则询问是否停止修改
                    if (MessageBox.Show("当前处在编辑状态，是否停止编辑并退出？\n当前内容不会被保留！", "警告：", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        //停止编辑的话，便退出
                        this.Close();
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        #region 隐藏某些复选框
        /// <summary>
        /// 隐藏根节点复选框
        /// </summary>
        private void TreeNodeCheckBoxs()
        {
            this.SemanticTree_TreeView.CheckBoxes = true;
            this.SemanticTree_TreeView.ShowLines = true;
            this.SemanticTree_TreeView.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
            this.SemanticTree_TreeView.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(tvCheck_DrawNode);
        }
        private void tvCheck_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {

            if (e.Node.Level == 0)   //需要隐藏checkbox的节点级别
            {
                HideCheckBox(this.SemanticTree_TreeView, e.Node);
            }

            e.DrawDefault = true;
        }

        private const int TVIF_STATE = 0x8;
        private const int TVIS_STATEIMAGEMASK = 0xF000;
        private const int TV_FIRST = 0x1100;
        private const int TVM_SETITEM = TV_FIRST + 63;
        private void HideCheckBox(TreeView tvw, TreeNode node)
        {
            TVITEM tvi = new TVITEM();
            tvi.hItem = node.Handle;
            tvi.mask = TVIF_STATE;
            tvi.stateMask = TVIS_STATEIMAGEMASK;
            tvi.state = 0;
            SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
        }

        [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
        private struct TVITEM
        {
            public int mask;
            public IntPtr hItem;
            public int state;
            public int stateMask;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpszText;
            public int cchTextMax;
            public int iImage;
            public int iSelectedImage; public int cChildren; public IntPtr lParam;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TVITEM lParam);
        #endregion

        
    }
}
